#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @Time : 2021/3/30 22:17 
# @Author : wenzi
# @File : __init__.py 
# @Software: PyCharm
from flask import Flask

from apiRestful.settings import DevelopConfig
from apiRestful.extensions import db
from apiRestful.api import api_bp


def register_blueprints(_app):
    _app.register_blueprint(api_bp)


def register_extensions(_app):
    db.init_app(_app)
    init_db(_app)


def init_db(_app: Flask):
    _app.app_context().push()
    # db.drop_all()
    db.create_all()
    db.session.commit()


def _create_app():
    _app = Flask(__name__)
    _app.config.from_object(DevelopConfig)

    register_blueprints(_app)
    register_extensions(_app)

    return _app


# 跨域支持
def after_request(response):
    # JS前端跨域支持
    response.headers['Cache-Control'] = 'no-cache'
    response.headers['Access-Control-Allow-Origin'] = '*'
    response.headers['Access-Control-Allow-Headers'] = '*'
    return response


app = _create_app()
app.after_request(after_request)
